<?php
namespace app\langjinmobile\controller;

use app\common\model\Business;
use app\common\model\User;
use app\common\model\GoodsCategory;
use app\common\model\Goods;
use app\common\model\Shoporder;

use think\Controller;
use think\Request;
use think\Validate;

use think\Db;

class MerchantOrder extends Controller
{
    /**
     * 显示资源列表
     * @return \think\Response
     */
    public function index()
    {
        $user_id = session('userid');
        $shop = Business::where('userid', $user_id)->find();
        $todeliver = Shoporder::where(['shops_id'=>$shop['id'],'status'=>1])->select();
        $ondelivering = Shoporder::where(['shops_id'=>$shop['id'],'status'=>2])->select();
        $hasdone = Shoporder::where(['shops_id'=>$shop['id'],'status'=>3])->select();
    
        $todeliver = $this->get_info($todeliver);
        $todeliver_anl = $this->get_anl($todeliver);

        $ondelivering = $this->get_info($ondelivering);
        $ondelivering_anl = $this->get_anl($ondelivering);
// dump($ondelivering);
// dump($ondelivering_anl);die;
        $hasdone = $this->get_info($hasdone);
        $hasdone_anl = $this->get_anl($hasdone);

        $data = ['status'=>1,'data'=>''];

        $this->assign('todeliver_anl',$todeliver_anl);
        $this->assign('ondelivering_anl',$ondelivering_anl);
        $this->assign('hasdone_anl',$hasdone_anl);

        return $this->fetch('', ['todeliver'=>$todeliver,
            'ondelivering'=>$ondelivering,
            'hasdone'=>$hasdone,
            'shop_info'=>$shop,
            'data'=>$data,
        ]);
    }

    /**
     * 订单商品详情
     * @return
     */
    public function get_info($order_list)
    {
        if(!$order_list)
            return ['status'=>0,'msg'=>'未找到相关数据'];

        $order =[];
        foreach ($order_list as $key => $value) { 
    
            $goods = Goods::where(['id'=>$value['goods_id']])->find()->toArray();
            $gimg = explode(',', $goods['img1']);
            $value['goodsimg'] = $gimg[0];
            $value['gname'] = $goods['name'];
            $value['sale_price'] = $goods['sale_price'];
            $value['sales'] = $goods['sales'];
            //订单编号一样为一个数组
            $order[$value['order_number']][] = $value;
        }

        return $order;
    }

    
    /**
     * 订单统计  商品数  总价
     * @return
     */
    public function get_anl($order)
    {
        //计算订单合并总价
        $anl = [];
        foreach ($order as $key => $value) {
            $sum_money = 0.00;
            $sum_integral = 0.00;            
            $tnum = 0;
            if(is_array($value))
            {
                foreach ($value as $k => $val) {
                    $sum_money += $val['wallet_num'];
                    $sum_integral += $val['integral_num'];

                    $tnum += $val['num'];
                }
                $anl[$key]['tnum'] = $tnum;
                $anl[$key]['sum_money'] = number_format($sum_money, 2);
                $anl[$key]['sum_integral'] = number_format($sum_integral, 2);                
            }     
        }

        return $anl;
    }

    /**
     * 发货逻辑
     */
    public function ship()
    {
        // 订单ID
        $user_id = session('userid');
        $shop_info = Business::where('userid', $user_id)->find();
        $order_number = request()->param('order_number');

        $order = Shoporder::where(['order_number'=>$order_number, 'shops_id'=>$shop_info['id']])->select();
        if(count($order) == 0)
        {
            $data = ['status'=>0, 'msg'=>'订单不存在'];
            return json($data);
        }
        //显示邮费
        $postage = 0;
        foreach ($order as $key => $value) {
            if($value['status'] == '确认收货')
            {
                $data = ['status'=>0, 'msg'=>'订单状态不允许'];
                return json($data);
            }

            if(!$value['username'] || !$value['mobile'] || !$value['address'])
            {
                $data = ['status'=>0, 'msg'=>'收货人信息不完整'];
                return json($data);
            }

            $goods = Goods::where(['id'=>$value['goods_id']])->find();
            $value['gname'] = $goods['name'];
            $value['dprice'] = $goods['sale_price'];
            $img = explode(',',$goods['img1']);
            $value['goodsimg'] = str_replace('\\', '/', $img[0]);
            $postage += $goods['postage'];
        }

        if(request()->isPost())
        {
            $ship_num = request()->param('ship_num');
            if(!$ship_num)
            {
                $data = ['status'=>0, 'msg'=>'运单号不能为空'];
                return json($data);
            }
            $is_valid = preg_match('/['.chr(0xa1).'-'.chr(0xff).']/', $ship_num);
            if($is_valid)
            {
                $data = ['status'=>0, 'msg'=>'运单号不合法'];
                return json($data);
            }
            $data['ship_num'] = $ship_num;
            $data['status'] = 2;
            $data['update_time'] = time();
            $update = Shoporder::where(['order_number'=>$order_number,'shops_id'=>$shop_info['id']])->update($data);

            if($update !== false)
            {
                return ['status'=>1, 'msg'=>'操作成功','url'=>url('MerchantOrder/index')];
            }
            else{
                return ['status'=>0, 'msg'=>'操作失败'];
            }
        }

        return $this->fetch('', ['order'=>$order,'shop_info'=>$shop_info, 'postage'=>$postage]);
        
    } 

    /**
     *  订单详情
     * @return
     */
    public function detail()
    {
        $user_id = session('userid');
        $order_number = request()->param('order_number');
        $shop_info = Business::where('userid', $user_id)->find();

        $order = Shoporder::where(['order_number'=>$order_number, 'shops_id'=>$shop_info['id']])->select();
        if(empty($order))
        {
            $data = ['status'=>0, 'msg'=>'订单不存在'];
            return json($data);
        }
        foreach ($order as $key => $value) {

            $goods = Goods::where(['id'=>$value['goods_id']])->find();

            $value['gname'] = $goods['name'];
            $value['dprice'] = $goods['sale_price'];
            $img = explode(',',$goods['img1']);
            $value['goodsimg'] = $img[0];
        }

        return $this->fetch('', ['order'=>$order,'shop_info'=>$shop_info]);
    }

}
